package com.xr.sunnyday.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xr.sunnyday.dao.MemberDao;
import com.xr.sunnyday.dao.OrderDao;
import com.xr.sunnyday.dao.OrderSettingDao;
import com.xr.sunnyday.exception.AssociationException;
import com.xr.sunnyday.pojo.Member;
import com.xr.sunnyday.pojo.Order;
import com.xr.sunnyday.pojo.OrderSetting;
import com.xr.sunnyday.service.OrderService;
import com.xr.sunnyday.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService{

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;
    /*
    * 通过日期**查询预约设置是否存在** t_ordersetting

     * 不存在就报错(可能不是工作日，医生后台关系系统没有设置该天)
     * 存在 则**判断是否约满**
     *  约满：报错
     *  没有约满：**判断是否为会员**，通过手机号码查询 t_member
     * 非会员：  添加用户已填信息到会员表，其他没有涉及到表中字段的信息，则让用户后期完善
     * 是会员：**判断是否重复预约** t_order 通过member_id,orderDate, setmeal_id
     * 重复：则报错
     * 没重复：可预约，**添加信息至订单表**，并且更新已预约数量 t_ordersetting
    * */

    @Override
    public Order submitOrder(Map<String, String> paramMap) {
        //1.日期转换
        //1.1获取提交的日期
        String date = paramMap.get("orderDate");
        //1.2通过DateUtils转换成日期格式
        Date orderDate=null;
        try {
            orderDate = DateUtils.parseString2Date(date);
        } catch (Exception e) {
            throw new AssociationException("日期转换错误");
        }

        //2.判断是否有这一天的预约设置信息
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(orderDate);
        if(null==orderSetting){
            throw new AssociationException("抱歉，该日期非工作日，不能预约");
        }

        //3.判断这一天是否约满
        if(orderSetting.getReservations()>=orderSetting.getNumber()){
            throw new AssociationException("该日期人数已约满，请更换");
        }

        //4.通过手机号查询是否为会员
        //4.1获取提交的手机号
        String telephone = paramMap.get("telephone");
        //4.2获取会员，为空添加
        Member member = memberDao.findByTelephone(telephone);
        if(null==member){
            member=new Member();
            member.setName(paramMap.get("name"));
            member.setSex(paramMap.get("sex"));
            member.setPhoneNumber(telephone);
            member.setIdCard(paramMap.get("idCard"));
            member.setRegTime(new Date());
            memberDao.add(member);
        }

        //5.判断是否重复预约
        //5.1通过提交的数据构建一个order对象
        Order order=new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(orderDate);
        order.setSetmealId(Integer.valueOf(paramMap.get("setmealId")));
        //5.2条件查询
        List<Order> orderList = orderDao.findByCondition(order);
        if(null!=orderList && orderList.size()>0){
            throw new AssociationException("已经预约了");
        }

        order.setOrderType(paramMap.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);

        //6.不存在，没重复,添加订单
        orderDao.add(order);
        //7.更新已预约数量 t_ordersetting
        orderSettingDao.editReservationsByOrderDate(orderSetting);

        return order;
    }

    @Override
    public Map<String, Object> findOrderDetailById(int id) {
        return orderDao.findById4Detail(id);
    }
}
